import { Fields, TableAction } from "@/types/public";
import { Button, Popconfirm, Space } from "antd";
import React from "react";
import { EditOutlined, DeleteOutlined } from "@ant-design/icons";

// 路由配置数据结构定义
export interface RouteConfigField {
  /** 路由ID */
  routeId: number;

  /** 路由名称 */
  routeName: string;

  /** 路由路径 */
  routePath: string;

  /** 父级ID */
  parentId: number;

  /** 权限标识 */
  permissionKey: string;

  /** 路由类型 */
  routeType: string;

  /** 图标 */
  icon: string;

  /** 排序 */
  sortOrder: number;

  /** 是否显示 */
  isShow: string;

  /** 是否保持活跃 */
  keepAlive: string;

  /** 创建时间 */
  createdAt: string | Date;

  /** 是否分组 */
  isGroup: string;

  /** 备注 */
  remarks: string;

  /** 路由别名 */
  routeAlias: string;

  /** 请求方法 */
  method: string;

  /** 是否需要认证 */
  isAuth: string;

  /** 更新时间 */
  updatedAt: string | Date;

  /** 删除时间 */
  deletedAt: string | Date;

  /** 是否开放 */
  isOpen: string;
}

export const createRouteConfigField = (
  tableAction: TableAction<RouteConfigField> = {}
): Fields[] => {
  return [
    {
      valueType: "string",
      inputType: "input",
      title: "路由别名",
      width: 90,
      visible: false,
      dataIndex: "routeAlias",
      key: "routeAlias",
      placeholder: "请输入路由别名",
      rules: [
        { required: false },
        { max: 100, message: "路由别名长度不能超过100字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "textarea",
      title: "备注",
      dataIndex: "remarks",
      visible: true,
      width: 180,
      key: "remarks",
      placeholder: "请输入备注信息",
      autoSize: { minRows: 2, maxRows: 4 },
      rules: [{ required: false }],
    },
    {
      valueType: "number",
      inputType: "input-number",
      key: "routeId",
      title: "路由ID",
      dataIndex: "routeId",
      placeholder: "请输入路由ID",
      disabled: true,
      width: 80,
      rules: [
        { required: true, message: "路由ID不能为空" },
        { type: "number", min: 1, message: "路由ID必须大于0" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "路由名称",
      width: 120,

      dataIndex: "routeName",
      key: "routeName",
      placeholder: "请输入路由名称",
      rules: [
        { required: true, message: "路由名称不能为空" },
        { max: 50, message: "路由名称长度不能超过50字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "路由路径",
      dataIndex: "routePath",
      width: 260,
      key: "routePath",
      placeholder: "请输入路由路径",
      rules: [
        { required: true, message: "路由路径不能为空" },
        { max: 255, message: "路由路径长度不能超过255字符" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "父级ID",
      dataIndex: "parentId",
      key: "parentId",
      placeholder: "请输入父级ID",
      visible: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "权限标识",
      dataIndex: "permissionKey",
      visible: false,
      key: "permissionKey",
      placeholder: "请输入权限标识",
      rules: [
        { required: false },
        { max: 100, message: "权限标识长度不能超过100字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "select",
      title: "路由类型",
      dataIndex: "routeType",
      key: "routeType",
      placeholder: "请选择路由类型",
      options: [
        { value: "menu", title: "菜单" },
        { value: "button", title: "按钮" },
        { value: "api", title: "接口" },
        // 可根据实际情况添加更多选项
      ],
      visible: false,
      rules: [
        { required: false },
        { max: 20, message: "路由类型长度不能超过20字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "图标",
      dataIndex: "icon",
      visible: false,
      key: "icon",
      placeholder: "请输入图标名称",
      rules: [
        { required: false },
        { max: 50, message: "图标名称长度不能超过50字符" },
      ],
    },
    {
      valueType: "number",
      inputType: "input-number",
      title: "排序",
      width: 90,
      dataIndex: "sortOrder",
      key: "sortOrder",
      placeholder: "请输入排序值",
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "是否显示",
      visible: true,
      width: 90,
      dataIndex: "isShow",
      key: "isShow",

      initialValue: true,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "保持活跃",
      dataIndex: "keepAlive",
      width: 90,
      key: "keepAlive",
      visible: false,
      initialValue: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "是否分组",
      width: 90,
      dataIndex: "isGroup",
      key: "isGroup",
      visible: false,
      initialValue: false,
      rules: [{ required: false }],
    },

    {
      valueType: "string",
      inputType: "select",
      title: "请求方法",
      dataIndex: "method",
      width: 90,
      key: "method",
      placeholder: "请选择请求方法",
      options: [
        { value: "GET", title: "GET" },
        { value: "POST", title: "POST" },
        { value: "PUT", title: "PUT" },
        { value: "DELETE", title: "DELETE" },
        { value: "PATCH", title: "PATCH" },
        { value: "OPTIONS", title: "OPTIONS" },
        { value: "HEAD", title: "HEAD" },
        { value: "ALL", title: "ALL" },
      ],
      rules: [
        { required: false },
        { max: 10, message: "请求方法长度不能超过10字符" },
      ],
    },
    {
      valueType: "string",
      inputType: "input",
      title: "需要认证",
      dataIndex: "isAuth",
      key: "isAuth",
      initialValue: true,
      rules: [{ required: false }],
      width: 90,
    },
    {
      valueType: "number",
      inputType: "radio",
      title: "是否开放",
      dataIndex: "isOpen",
      key: "isOpen",
      visible: false,
      width: 90,
      initialValue: false,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "创建时间",
      dataIndex: "createdAt",
      key: "createdAt",
      width: 200,
      visible: false,
      disabled: true,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "更新时间",
      width: 200,
      visible: false,
      dataIndex: "updatedAt",
      key: "updatedAt",
      disabled: true,
      rules: [{ required: false }],
    },
    {
      valueType: "string",
      inputType: "date-picker",
      title: "删除时间",
      dataIndex: "deletedAt",
      visible: false,
      key: "deletedAt",
      disabled: true,
      rules: [{ required: false }],
    },
    {
      title: "操作",
      key: "action",
      fixed: "right",
      width: 200,
      render: (_: any, record: RouteConfigField) => (
        <Space size="middle">
          <Button
            type="link"
            size="small"
            icon={<EditOutlined />}
            onClick={() => tableAction.edit?.(record)}
          >
            编辑
          </Button>
          <Popconfirm
            title="确定删除此路由配置？"
            description="删除配置将无法恢复"
            onConfirm={() => tableAction.delete?.([record.routeId])}
          >
            <Button type="link" danger icon={<DeleteOutlined />}>
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];
};
